package com.youhong.cuptime.blesdk;

import android.content.Context;
import android.content.Intent;

import java.util.Calendar;

/**
 * Created by Administrator on 2017/10/6.
 */

public class BabyBrushBleOperation extends BaseBleOperation {

    public BabyBrushBleOperation(Context context) {
        super(context);
    }

    @Override
    protected void Action_RX_data(Context context, Intent intent) {

        byte[] values = intent.getByteArrayExtra(LightBLEService.EXTRA_DATA);

        if (values[0] == (byte) 0x7) {


            if (brushingCallback != null) {

                int count = (values[1] & 0xFF) + ((values[2] << 8) & 0xFF00);
                brushingCallback.brushingCallback(count);
            }

        } else if (values[0] == (byte) 0xBC) {

            if (buttonPressedCallback != null) {

                buttonPressedCallback.onButtonPressedCallback();
            }
        } else if (values[0] == (byte) 0xF9) {

            /*例：2015/08/14 09:46,则读取格式如下：
            0xF9+0x09+0x2E(46)+0x08+0x0e(14)+0xdf+0x07(2015)*/
            if (getTimeCallback != null) {

                int hour, minute, month, day, year;

                hour = values[1];
                minute = values[2];
                month = values[3];
                day = values[4];
                year = values[5] + ((values[6] & 0xFF) << 8);

                Calendar c = Calendar.getInstance();
                c.set(Calendar.YEAR, year);
                c.set(Calendar.MONTH, month - 1);
                c.set(Calendar.DAY_OF_MONTH, day);
                c.set(Calendar.HOUR_OF_DAY, hour);
                c.set(Calendar.MINUTE, minute);
                getTimeCallback.onGetTime(c);
            }
        }
    }

    public void checkPassword(String password) {

        byte[] values = new byte[7];
        values[0] = (byte) 0xFD;
        Util.copyByteArray(password.getBytes(), values, 0, 1, 6);
        super.writeCheck(values);


    }

    public void alterPassword(String password) {

        byte[] values = new byte[7];
        values[0] = (byte) 0xFE;
        Util.copyByteArray(password.getBytes(), values, 0, 1, 6);
        super.writeCheck(values);

    }

    public void setTime(Calendar c) {

        byte[] values = new byte[13];
        values[0] = (byte) 0xFA;
        int year = c.get(Calendar.YEAR);
        //Copy Year
        Util.copyByteArray(Util.getBytesBCDFromInt(year, 4), values, 0, 1, 4);

        int month = c.get(Calendar.MONTH) + 1;

        Util.copyByteArray(Util.getBytesBCDFromInt(month, 2), values, 0, 5, 2);

        int day = c.get(Calendar.DAY_OF_MONTH);

        Util.copyByteArray(Util.getBytesBCDFromInt(day, 2), values, 0, 7, 2);

        int hour = c.get(Calendar.HOUR_OF_DAY);

        Util.copyByteArray(Util.getBytesBCDFromInt(hour, 2), values, 0, 9, 2);

        int minutes = c.get(Calendar.MINUTE);

        Util.copyByteArray(Util.getBytesBCDFromInt(minutes, 2), values, 0, 11, 2);

        super.writeTx(values);

    }

    public void getTime(GetTimeCallback callback) {

        getTimeCallback = callback;
        byte[] values = new byte[2];
        values[0] = (byte) 0xF9;
        values[1] = (byte) 0x55;
        super.writeTx(values);
    }

    public static final byte BREATHE_LIGHT_RED = 0x4;
    public static final byte BREATHE_LIGHT_GREEN = 0x3;

    public void lightOn(byte light, byte lightingTime) {

        byte[] values = new byte[4];
        values[0] = (byte) 0xF8;
        values[1] = light;
        values[2] = lightingTime;
        super.writeTx(values);
    }

    public void clearBrushingCount() {

        byte[] values = new byte[3];
        values[0] = (byte) 0xFF;
        values[1] = (byte) 0x46;
        values[2] = (byte) 0x15;
        super.writeTx(values);
    }

    GetTimeCallback getTimeCallback;

    public interface GetTimeCallback {

        void onGetTime(Calendar c);

    }

    public void setBrushingCallback(BrushingCallback brushingCallback) {
        this.brushingCallback = brushingCallback;
    }

    BrushingCallback brushingCallback = null;

    public interface BrushingCallback {

        void brushingCallback(int count);

    }

    public void setButtonPressedCallback(ButtonPressedCallback buttonPressedCallback) {
        this.buttonPressedCallback = buttonPressedCallback;
    }

    ButtonPressedCallback buttonPressedCallback = null;

    public interface ButtonPressedCallback {

        void onButtonPressedCallback();
    }
}